/*
 * Copyright (C) 2017 The MoonLake Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package com.minecraft.moonlake.legendaryweaponrel.listeners;

import com.minecraft.moonlake.api.event.MoonLakeListener;
import com.minecraft.moonlake.legendaryweaponrel.LegendaryWeaponRelPlugin;
import com.minecraft.moonlake.legendaryweaponrel.weapons.Weapons;
import com.minecraft.moonlake.legendaryweaponrel.weapons.armor.*;
import com.minecraft.moonlake.legendaryweaponrel.weapons.bow.HealBow;
import com.minecraft.moonlake.legendaryweaponrel.weapons.bow.Rain;
import com.minecraft.moonlake.legendaryweaponrel.weapons.bow.SlowBow;
import com.minecraft.moonlake.legendaryweaponrel.weapons.bow.VoidBow;
import com.minecraft.moonlake.legendaryweaponrel.weapons.potion.Hail;
import com.minecraft.moonlake.legendaryweaponrel.weapons.potion.Might;
import com.minecraft.moonlake.legendaryweaponrel.weapons.potion.Tune;
import com.minecraft.moonlake.legendaryweaponrel.weapons.tool.Deal;
import com.minecraft.moonlake.legendaryweaponrel.weapons.tool.LongSword;
import com.minecraft.moonlake.legendaryweaponrel.weapons.tool.Sacrificial;
import com.minecraft.moonlake.legendaryweaponrel.weapons.tool.ThieveSword;
import org.bukkit.Material;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityShootBowEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;

public class PlayerListener implements MoonLakeListener {

    private final LegendaryWeaponRelPlugin main;

    public PlayerListener(LegendaryWeaponRelPlugin main) {
        this.main = main;
    }

    public LegendaryWeaponRelPlugin getMain() {
        return main;
    }

    @EventHandler
    public void onQuit(PlayerQuitEvent event) {
        // 处理玩家退出事件
        Player player = event.getPlayer();

        // Deal 武器处理
        Weapons.DEAL.onQuit(player).accept(event);
        // OverKill 武器处理
        Weapons.OVERKILL.onQuit(player).accept(event);
        // LongSword 武器处理
        Weapons.LONGSWORD.onQuit(player).accept(event);
    }

    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
        // 处理玩家死亡事件
        Player player = event.getEntity();

        // Deal 武器处理
        Weapons.DEAL.onDeath(player).accept(event);
    }

    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
        // 处理玩家交互事件
        Player player = event.getPlayer();
        ItemStack handItem = player.getItemInHand();

        if(handItem == null || handItem.getType() == Material.AIR) return;
        if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            // 只有右键交互才进入的武器处理

            // 处理传说武器护甲的玩家手持交互
            WeaponArmor weaponArmor = Weapons.getWeaponArmor(player, handItem);
            if(weaponArmor != null) weaponArmor.onConsumer(player).accept(event);

            // 处理 Sacrificial
            Sacrificial sacrificial = Weapons.getWeapon(Sacrificial.class, player, handItem);
            if(sacrificial != null) sacrificial.onConsumer(player).accept(event);
            // 处理 Deal
            Deal deal = Weapons.getWeapon(Deal.class, player, handItem);
            if(deal != null) deal.onConsumer(player).accept(event);
            // 处理 LongSword
            LongSword longSword = Weapons.getWeapon(LongSword.class, player, handItem);
            if(longSword != null) longSword.onConsumer(player).accept(event);
        }
    }

    @EventHandler
    public void onInteractEntity(PlayerInteractEntityEvent event) {
        // 处理玩家交互实体事件
        Player player = event.getPlayer();
        ItemStack handItem = player.getItemInHand();

        // 处理 ThieveSword
        ThieveSword thieveSword = Weapons.getWeapon(ThieveSword.class, player, handItem);
        if(thieveSword != null) thieveSword.onConsumer(player).accept(event);
    }

    @EventHandler
    public void onShoot(EntityShootBowEvent event) {
        // 处理实体射箭事件
        if(!(event.getEntity() instanceof Player)) return; // 不是玩家射箭则返回

        Player player = (Player) event.getEntity();
        ItemStack handItem = player.getItemInHand();

        // 处理 Rain
        Rain rain = Weapons.getWeapon(Rain.class, player, handItem);
        if(rain != null) rain.onConsumer(player).accept(event);
        // 处理 SlowBow
        SlowBow slowBow = Weapons.getWeapon(SlowBow.class, player, handItem);
        if(slowBow != null) slowBow.onConsumer(player).accept(event);
        // 处理 HealBow
        HealBow healBow = Weapons.getWeapon(HealBow.class, player, handItem);
        if(healBow != null) healBow.onConsumer(player).accept(event);
        // 处理 VoidBow
        VoidBow voidBow = Weapons.getWeapon(VoidBow.class, player, handItem);
        if(voidBow != null) voidBow.onConsumer(player).accept(event);
    }

    @EventHandler(priority = EventPriority.LOWEST)
    public void onDamage(EntityDamageByEntityEvent event) {
        // 处理玩家受伤是实体所为事件
        Entity damager = event.getDamager();
        Entity entity = event.getEntity();

        if(damager instanceof Arrow && ((Arrow) damager).getShooter() instanceof Player) {
            // 攻击者实体为弓箭并且弓箭由玩家射出
            Arrow arrow = (Arrow) damager;

            // 处理 Rain
            Weapons.RAIN.onConsumerArrow(arrow, entity).accept(event);
            // 处理 SlowBow
            Weapons.SLOWBOW.onConsumerArrow(arrow, entity).accept(event);
            // 处理 HealBow
            Weapons.HEALBOW.onConsumerArrow(arrow, entity).accept(event);
            // 处理 VoidBow
            Weapons.VOIDBOW.onConsumerArrow(arrow, entity).accept(event);

        } else if(damager instanceof LivingEntity && entity instanceof Player) {
            // 否则攻击者实体为活着的实体并且受伤者是玩家
            Player entityPlayer = (Player) entity;

            // 处理 Bind
            Bind bind = Weapons.getWeapon(Bind.class, entityPlayer, entityPlayer.getInventory().getHelmet()); // 头盔
            if(bind != null) bind.onConsumerArmor(entityPlayer).accept(event);
            // 处理 Tempest
            Tempest tempest = Weapons.getWeapon(Tempest.class, entityPlayer, entityPlayer.getInventory().getChestplate()); // 护甲
            if(tempest != null) tempest.onConsumerArmor(entityPlayer).accept(event);
            // 处理 Tempest
            Terror terror = Weapons.getWeapon(Terror.class, entityPlayer, entityPlayer.getInventory().getChestplate()); // 护甲
            if(terror != null) terror.onConsumerArmor(entityPlayer).accept(event);
            // 处理 Wrath
            Wrath wrath = Weapons.getWeapon(Wrath.class, entityPlayer, entityPlayer.getInventory().getChestplate()); // 护甲
            if(wrath != null) wrath.onConsumerArmor(entityPlayer).accept(event);
            // 处理 NinjaSandals
            NinjaSandals ninjaSandals = Weapons.getWeapon(NinjaSandals.class, entityPlayer, entityPlayer.getInventory().getBoots()); // 鞋子
            if(ninjaSandals != null) ninjaSandals.onConsumerArmor(entityPlayer).accept(event);
            // 处理 RubberShield
            RubberShield rubberShield = Weapons.getWeapon(RubberShield.class, entityPlayer, entityPlayer.getInventory().getChestplate()); // 护甲
            if(rubberShield != null) rubberShield.onConsumerArmor(entityPlayer).accept(event);
        }
    }

    @EventHandler
    public void onConsumer(PlayerItemConsumeEvent event) {
        // 处理玩家物品消费事件
        Player player = event.getPlayer();
        ItemStack handItem = event.getItem();

        // 处理 Hail
        Hail hail = Weapons.getWeapon(Hail.class, player, handItem);
        if(hail != null) hail.onConsumer(player).accept(event);
        // 处理 Might
        Might might = Weapons.getWeapon(Might.class, player, handItem);
        if(might != null) might.onConsumer(player).accept(event);
        // 处理 Tune
        Tune tune = Weapons.getWeapon(Tune.class, player, handItem);
        if(tune != null) tune.onConsumer(player).accept(event);
    }

    @EventHandler
    public void onClick(InventoryClickEvent event) {
        // 处理玩家物品栏点击事件
        if(event.getClickedInventory() == null) return; // 点击物品栏为 null 返回
        if(event.getClickedInventory().getType() != InventoryType.PLAYER) return; // 点击物品栏类型不为玩家则返回
        if(!(event.getWhoClicked() instanceof Player)) return; // 点击的不为玩家则返回

        Player player = (Player) event.getWhoClicked();
        ItemStack itemStack = event.getCurrentItem();

        // 处理传说武器护甲的玩家背包点击
        WeaponArmor weaponArmor = Weapons.getWeaponArmor(player, itemStack);
        if(weaponArmor != null) weaponArmor.onConsumerClick(player).accept(event);
    }
}
